package com.zm.study.common.memcache;


import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

/**
 * 使用memcached的缓存实用类.
 * 
 * @author zoulei
 * 
 */
public class MemCached {
	// 创建全局的唯一实例
	private static final Logger LOG = LoggerFactory.getLogger(MemCached.class);

	protected static MemCachedClient mcc = null;

	private volatile static MemCached instance = null;

	private SockIOPool pool = null;

	private String poolName = "sidsock";

	private Integer timeOut = 30;

	private MemCached() {
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		InputStream infile = cl.getResourceAsStream("memCache.properties");
		Properties props = new Properties();
		try {
			props.load(infile);
			String serverlist = props.getProperty("serverlist");
		    String weight=props.getProperty("weight");
			poolName = props.getProperty("poolname");
			timeOut = Integer.valueOf(props.getProperty("timeOut"));

			String[] servers = serverlist.split(",");
			Integer[] weights  = new Integer[servers.length];
			String[] strWeights =  weight.split(",");
			if(servers.length==strWeights.length){
				for(int i=0;i<servers.length;i++){
					weights[i] = Integer.valueOf(strWeights[i]);
				}
			}else{
				for(int i=0;i<servers.length;i++){
					weights[i] = 1;
				}
			}
	
			pool = SockIOPool.getInstance(poolName);
			pool.setServers(servers);
			pool.setWeights(weights);
	
	
	
	
			pool.setInitConn(10);
			pool.setMinConn(5);
			pool.setMaxConn(250);
			pool.setMaxIdle(1000 * 60 * 60 * 6);
	
			pool.setMaintSleep(30);
			pool.setNagle(false);
	//		pool.setNagle(true);
	//		pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);
	//		pool.setFailover(true);
	
			pool.setSocketTO(3000);
			pool.setSocketConnectTO(0);
	//		pool.setAliveCheck(true);
			pool.initialize();
			mcc = new MemCachedClient(poolName);
			LOG.info("======================初始化MemCached完成！= serverlist {}===poolName {}===================", serverlist, poolName);
		} catch (Exception e) {
			LOG.error("MemCached服务器连接失败！",e);
		}

	}

	/**
	 * 获取唯一实例.
	 * 
	 * @return
	 */
	public static MemCached getInstance() {
		if(instance == null){
			synchronized (MemCached.class){
				if(instance == null){
					instance = new MemCached();
				}
			}
		}
		return  instance;
	}

	/**
	 * 添加一个指定的值到缓存中.
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean add(String key, Object value) {
		return mcc.add(key, value);
	}
	public boolean add(String key, Object value, Date expiry) {
		return mcc.add(key, value, expiry);
	}
	public boolean add(String key, Object value, Integer timeoutSeconds){
		return mcc.add(key, value, timeoutSeconds);
	}

	/**
	 * 修改
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean replace(String key, Object value) {
		return mcc.replace(key, value);
	}

	public boolean replace(String key, Object value, Date expiry) {
		return mcc.replace(key, value, expiry);
	}

	public boolean replace(String key, Object value, Integer timeoutSeconds){
		return mcc.replace(key, value, timeoutSeconds);
	}

	public boolean set(String key, Object value, Integer timeout){
		return mcc.set(key,value,timeout);
	}
	
	public boolean set(String key, Object value, Date expiry) {
		return mcc.set(key,value,expiry);
	}

	/**
	 * 根据指定的关键字获取对象.
	 *
	 * @param key
	 * @return
	 */
	public Object get(String key) {
		return mcc.get(key);
	}
	/**
	 * 删除
	 * @param key
	 * @return
	 */
	public Object delete(String key) {
		return mcc.delete(key);
	}

	public static void main(String[] args) {
		MemCached cache = MemCached.getInstance();
		cache.delete("userinfo/common/user_indexMessageUtf8ServicemesssageKey_link_pagetrue");
		cache.delete("MessageUtf8ServicemesssageKey");
		/*long s = 123213;
		cache.add("hello", s);
		System.out.print("get value : " + cache.get("hello"));
		cache.delete("hello");
		System.out.print("get value : " + cache.get("hello"));
		cache.replace("test", "132132132");
		System.out.println(cache.get("test"));*/
	}
}
